/*
 * @Author: ChenZH 
 * @Date: 2019-07-07 16:48:47 
 * @Last Modified by: ChenZH
 * @Last Modified time: 2019-11-06 11:04:20
 */

const http = require('http');
const fs = require('fs');

const param = '{configId: 1000}';

const filePath = `src/app/services/config.ts`;

const req = http.request({

    hostname: '192.168.1.34',
    port: 8801,
    path: '/api/core/config/getApiConfigInfo',
    method: 'POST',
    headers: {
        'Content-Type': 'application/json-patch+json',
        'Ver': 'web_1.0',
    },
}, (res) => {
    fs.writeFile(filePath, 'export const apiUrls = ', () => {
        console.info(`successfully saved config.ts at ${filePath}`);
    });
    res.setEncoding('utf8');
    let responseText = '';
    res.on('data', (chunk) => {
        responseText += chunk;
    });
    res.on('end', () => {
        let data = JSON.parse(responseText);
        fs.appendFile(filePath, JSON.stringify(data, '', '   '), () => {
            console.info(`successfully appendFile config.ts at ${filePath}`);

            fs.appendFile(filePath, '\n', () => {});
        });
    });
});
req.on('error', (e) => {
    console.error(`problem with request: ${e.message}`);
});
req.write(param);
req.end();